package com.situ.stl.controller;

import com.situ.stl.entity.Result;
import com.situ.stl.util.CaptchaUtil;
import com.situ.stl.util.JWTUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 */
@RestController
@CrossOrigin
@RequestMapping( value = "/captcha")
public class CaptchaController {

    /**
     * 验证码
     */
    @GetMapping
    public Result captcha(){
        // 生成一个随机字符串
        String captcha = CaptchaUtil.randomCode(5);
        // 生成Base64格式的图片
        String img = CaptchaUtil.createBase64(captcha);
        // 生成 key, 10分钟有效
        String key = JWTUtil.createJWT(captcha, 10*60*1000L);

        return Result.success(0, key, img);
    }

}
